/**
 * 
 */
package com.demo.android.bmi_mvc;

import java.text.DecimalFormat;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

/**
 * @author Taohuang
 *
 */
public class Report extends Activity {

	/**
	 * 
	 */
	private static final String TAG = "Bmi";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.report);
		
		findViews();
		setListeners();
		showResults();
		Log.v(this.toString(),"onCreate");
		//listen for button clicks
		//Button button = (Button)findViewById(R.id.submit);
		//button.setOnClickListener(calcBMI);
		
	}
	
	private Button button_back;
	private TextView view_result;
	private TextView view_suggest;
	
	private void findViews(){
		button_back = (Button) findViewById(R.id.report_back);
		view_result = (TextView) findViewById(R.id.result);
		view_suggest = (TextView) findViewById(R.id.suggest);
		Log.d(this.toString(),"find view");
	}
	
	private void setListeners(){
		button_back.setOnClickListener(backMain);
	}
	
	private Button.OnClickListener backMain =new Button.OnClickListener(){

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Report.this.finish();
		}
	};
	
	protected void showNotification (double BMI) {
		NotificationManager barManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		
		Notification barMsg = new Notification(
				R.drawable.icon,getString(R.string.notification_bar),System.currentTimeMillis());
		
		PendingIntent contentIntent = PendingIntent.getActivity(
				this, 
				0,
				new Intent(this,Bmi_mvc.class), 
				PendingIntent.FLAG_UPDATE_CURRENT);
		
		barMsg.setLatestEventInfo(Report.this, "你的BMI值过高", "通知家长", contentIntent);
		
		barManager.notify(0, barMsg);
	}
	
	private void showResults(){
		DecimalFormat nf = new DecimalFormat("0.00");
		try {
			
		
		
		Bundle bundle = this.getIntent().getExtras();
		double height = Double.parseDouble(bundle.getString("KEY_HEIGHT"))/100;
		double weight = Double.parseDouble(bundle.getString("KEY_WEIGHT"));
		double BMI = weight/(height*height);
		
		view_result.setText(getString(R.string.bmi_result)+nf.format(BMI));
		
		if (BMI>25) {
			view_suggest.setText(R.string.advice_heavy);
			showNotification(BMI);
		} else if (BMI<20){
			view_suggest.setText(R.string.advice_light);
		} else {
			view_suggest.setText(R.string.advice_average);
		}
		
		}//end try
		catch(Exception err){
			Log.e(this.toString(),"error:"+err.toString());
			Toast.makeText(getApplicationContext(), R.string.input_error, Toast.LENGTH_SHORT).show();
		}
		
	}

}
